import { Directive, Injectable } from '@angular/core'
import { ICellRendererAngularComp } from 'ag-grid-angular'
import { ICellRendererParams } from 'ag-grid-community'

/**
 * 单元格组件基类 默认暴露以下几个属性 使调用代码简短 支持泛型 有更好的类型提示
 */
@Directive()
@Injectable()
export class AdBaseGridCellComponent<T = any, D = any> implements ICellRendererAngularComp {
  params: ICellRendererParams
  refresh(): boolean {
    return false
  }
  agInit(params: ICellRendererParams): void {
    this.params = params
  }

  /**
   * 字段对应值
   */
  get value() {
    return this.params.value
  }

  /**
   * 行数据对象
   */
  get data(): D {
    return this.params.data
  }

  /**
   * 节点对象
   */
  get node() {
    return this.params.node
  }

  get parent() {
    return this.componentParent
  }

  /**
   * 父组件实例对象
   */
  get componentParent(): T {
    const { componentParent, parent } = this.params.context
    return parent || componentParent
  }
}
